package com.wcl.demo.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wcl.demo.R;

import java.io.IOException;
import java.util.List;

/**
 * Created by dllo on 15/11/26.
 */
public class TakePhotoActivity extends Activity implements View.OnClickListener, SurfaceHolder.Callback {
    private SurfaceView surfaceView;
    private SurfaceHolder mHolder;
    private Camera camera;
    private ImageView take_picture_Iv;
    private TextView activity_take_photo_bacBtn;
    private String wayKey;
//    private Bitmap bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);
        initView();
        wayKey=getIntent().getStringExtra("wayKey");
        Log.i("shaco","------->>"+wayKey);
    }

    private void initView() {
        activity_take_photo_bacBtn= (TextView) findViewById(R.id.activity_take_photo_bacBtn);
        activity_take_photo_bacBtn.setOnClickListener(this);


        take_picture_Iv= (ImageView) findViewById(R.id.take_picture_Iv);
        surfaceView= (SurfaceView) findViewById(R.id.surfaceView);
        mHolder=surfaceView.getHolder();

        take_picture_Iv.setOnClickListener(this);
        mHolder.addCallback(this);
    }
    public void initCamera(){
        camera = Camera.open();
        if (camera != null) {
            //角度纠正
            camera.setDisplayOrientation(90);

            Camera.Parameters parameters=camera.getParameters();
            List<Camera.Size> sizes=parameters.getSupportedPictureSizes();
            for (Camera.Size s:sizes){
                Log.i("shaco", s.width + "------>>" + s.height);
            }


            if (sizes.size()>1){
//                Camera.Size size=sizes.get(sizes.size());
                Camera.Size size=sizes.get(sizes.size()/2);
                parameters.setPictureSize(size.width,size.height);
            }
            camera.setParameters(parameters);



            try {
                camera.setPreviewDisplay(mHolder);
                camera.startPreview();//开启预览
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 释放摄像头
     */
    public void releaseCamera() {
        if (camera != null) {
            camera.stopPreview();
            camera.release();
            camera = null;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.take_picture_Iv:
                camera.takePicture(null,null,new MyPictureCallBack());
                break;
            case R.id.activity_take_photo_bacBtn:
                finish();
                break;
        }

    }
    /**
     * 拍照返回的数据监听   数据格式为jpeg
     */
    public class MyPictureCallBack implements Camera.PictureCallback{

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            if (data!=null) {
//                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                Log.i("shaco", data + "");
                Intent intent =new Intent(TakePhotoActivity.this, PreviewPhotoActivity.class);
                intent.putExtra("bitmap",data);
                intent.putExtra("wayKey",wayKey);
                startActivity(intent);
                finish();
            }
            //camera.startPreview();//再次启动预览

        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        initCamera();//初始化摄像头对象

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseCamera();

    }
}
